/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.util;

public class NetUtil {

	private static char[] toReplace = { '\u0000', '\u0001', '\u0002', '\u0003',
			'\u0004', '\u0005', '\u0006', '\u0007', '\u0008', '\u000b',
			'\u000c', '\u000e', '\u000f', '\u0010', '\u0011', '\u0012',
			'\u0013', '\u0014', '\u0015', '\u0016', '\u0017', '\u0018',
			'\u0019', '\u001a', '\u001b', '\u001c', '\u001d', '\u001e',
			'\u001f' };

	public static String convertToHex(String line) {
		StringBuffer temp = new StringBuffer();
		byte[] bytes = new byte[line.length()];
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = (byte)line.charAt(i);
		}
		
		String str = "";
		for (int i = 0; i < bytes.length; i++) {
			str = Integer.toHexString(bytes[i]);
			if (str.length() == 1)
				str = "0" + str;
			temp.append(str);
		}
		return temp.toString();
	}

	
	public static String getProtocolName(int proto) {
		String protoStr = "" + proto;
		if (proto == 6)
			protoStr = "TCP";
		else if (proto == 1)
			protoStr = "ICMP";
		else if (proto == 17)
			protoStr = "UDP";
        else if (proto == -1)
            protoStr = "Unknown";

		return protoStr;
	}

	public static int getProtocolValue(String protoStr) {
		int proto = -1;
		protoStr = protoStr.toUpperCase();
		try {
			proto = Integer.parseInt(protoStr);
			return proto;
		} catch (Exception E) {

		}

		if (protoStr.equals("TCP"))
			proto = 6;
		else if (protoStr.equals("ICMP"))
			proto = 1;
		else if (protoStr.equals("UDP"))
			proto = 17;
		return proto;
	}

	public static String convertLongToIP(long ip) {
		String ipStr = Long.toHexString(ip);
		while (ipStr.length() < 8)
			ipStr = "0" + ipStr;
		return convertHexToIP(ipStr);
	}

	public static String convertHexToIP(String hex) {
		StringBuffer temp = new StringBuffer();
		temp.append(Integer.parseInt(hex.substring(0, 2), 16));
		if (hex.length() == 2) {
			temp.append(".0.0.0");
			return temp.toString();
		}
		temp.append(".");
		temp.append(Integer.parseInt(hex.substring(2, 4), 16));
		if (hex.length() == 4) {
			temp.append(".0.0");
			return temp.toString();
		}
		temp.append(".");
		temp.append(Integer.parseInt(hex.substring(4, 6), 16));
		if (hex.length() == 6) {
			temp.append(".0");
			return temp.toString();
		}
		temp.append(".");
		temp.append(Integer.parseInt(hex.substring(6), 16));
		return temp.toString();
	}

	public static short[] convertIPtoOctets(String ip) {
		String[] tokens = ip.split("\\.");
		int numTokens = tokens.length;
		if (numTokens != 4)
			return null;
		short[] temp = new short[numTokens];
		String val = "";
		for (int i = 0; i < numTokens; i++) {
			val = tokens[i];
			temp[i] = Short.parseShort(val);
		}
		return temp;
	}

	public static String convertIPtoHex(String ip) {
		short[] ipArray = NetUtil.convertIPtoOctets(ip);
		if (ipArray == null)
			return null;

		String temp = "";
		String hex = "";
		for (int i = 0; i < ipArray.length; i++) {
			hex = Integer.toHexString((int) ipArray[i]);
			if (hex.length() > 2)
				hex = hex.substring(hex.length() - 2);
			while (hex.length() < 2)
				hex = "0" + hex;
			temp = temp + hex;
		}
		return temp;
	}

	public static long convertIPtoLong(String ip) {
		return Long.parseLong(NetUtil.convertIPtoHex(ip), 16);

	}

	public static String xmlify(String str) {
		int idx = 0;

		while (idx < str.length()) {
			char c = str.charAt(idx);
			switch (c) {
			case '>':
				str = str.substring(0, idx) + "&gt;" + str.substring(idx + 1);
				idx += 4;
				break;
			case '<':
				str = str.substring(0, idx) + "&lt;" + str.substring(idx + 1);
				idx += 4;
				break;
			case '&':
				str = str.substring(0, idx) + "&amp;" + str.substring(idx + 1);
				idx += 5;
				break;
			/*
			 * case ' ' : str = str.substring(0, idx) + "&nbsp;" +
			 * str.substring(idx + 1); idx += 6; break;
			 */
			case '"':
				str = str.substring(0, idx) + "&quot;" + str.substring(idx + 1);
				idx += 6;
				break;
			case '\'':
				str = str.substring(0, idx) + "&apos;" + str.substring(idx + 1);
				idx += 6;
				break;
			default:
				idx++;
				break;

			}
		}
		str = NetUtil.truncateUnicode(str);
		return str;
	}

	public static String truncateUnicode(String str) {
		String temp = str;
		int idx = str.length();
		for (int i = 0; i < toReplace.length; i++) {
			int idx2 = temp.indexOf(toReplace[i]);
			if ((idx2 != -1) && (idx2 < idx)) {
				idx = idx2;
			}
		}
		if (idx != -1) {
			return temp.substring(0, idx);
		}
		return temp;
	}

}
